<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>点分治 学习笔记 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="其实就是在树上跑分治">
<meta property="og:type" content="article">
<meta property="og:title" content="点分治 学习笔记">
<meta property="og:url" content="http://example.com/2021/08/11/%E7%82%B9%E5%88%86%E6%B2%BB%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="其实就是在树上跑分治">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/8cs5olw0.png">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/t8wry7kh.png">
<meta property="article:published_time" content="2021-08-11T07:15:20.777Z">
<meta property="article:modified_time" content="2023-07-19T11:50:17.249Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.luogu.com.cn/upload/image_hosting/8cs5olw0.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-点分治 学习笔记" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/08/11/%E7%82%B9%E5%88%86%E6%B2%BB%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="article-date">
  <time class="dt-published" datetime="2021-08-11T07:15:20.777Z" itemprop="datePublished">2021-08-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      点分治 学习笔记
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>其实就是在树上跑分治</p>
<h2 id="0-点分治的用途"><a href="#0-点分治的用途" class="headerlink" title="0. 点分治的用途"></a>0. 点分治的用途</h2><p>点分治可以解决树上的关于路径的问题，例如 <a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P4178">洛谷P4178 Tree</a>。（题目大意：给定一棵 $n$ 个节点的树，每条边有边权，求出树上两点距离小于等于 $k$ 的点对数量）这道题如果使用 $O(n^2)$ 的暴力算法 <del>显然</del> 会T飞 ，然而之后您就会看到，点分治算法可以在 $O(n\log^2 n)$ 的时间复杂度内解决它。</p>
<h2 id="1-思想"><a href="#1-思想" class="headerlink" title="1.思想"></a>1.思想</h2><p>顾名思义，点分治使用了分治的思想，把原问题拆分成若干个子问题，分别求解后再合并。<del>把大象装进冰箱里需要几步？</del></p>
<h3 id="1-1-分"><a href="#1-1-分" class="headerlink" title="1.1. 分"></a>1.1. 分</h3><p>注意到如果指定一个节点为根节点，那么一个路径有可能有以下两个来源：</p>
<p>1.<img src="https://cdn.luogu.com.cn/upload/image_hosting/8cs5olw0.png" alt="">路径经过根节点；<br>2.<img src="https://cdn.luogu.com.cn/upload/image_hosting/t8wry7kh.png" alt="">路径完全被子树包含。</p>
<p>到这里一个分治算法已经呼之欲出了——可以递归地处理第二种情况，只需要在算法中考虑第一种情况就可以了。</p>
<h3 id="1-2-治"><a href="#1-2-治" class="headerlink" title="1.2. 治"></a>1.2. 治</h3><p>第二种情况可以递归地处理，并且递归到叶子节点时就不需要考虑第二种情况了（根本没有子树），所以这里主要考虑第一种情况。</p>
<p>按顺序考虑每一个子树，用一个树状数组（或者是一个别的什么数据结构）来维护根节点到已经考虑过的每一个节点，在新加入一个子树时对于新子树的每一个节点求出有多少个根节点到“老节点”的距离小于 （$k-$ （根节点到新节点的距离））并统计进答案，再把每一个新节点塞进树状数组里。这样就解决了第一种情况了。</p>
<p>最好结合代码理解：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//mark:树状数组 g:图</span></span><br><span class="line"><span class="type">int</span> dis[MAXN+<span class="number">5</span>],tail;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">get_dis</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> fa,<span class="type">int</span> now)</span></span>&#123;</span><br><span class="line">    dis[++tail]=now;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i].v,w=g[u][i].w;</span><br><span class="line">        <span class="keyword">if</span>(!removed[v]&amp;&amp;v!=fa)&#123;</span><br><span class="line">            <span class="built_in">get_dis</span>(v,u,now+w);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">calc</span><span class="params">(<span class="type">int</span> u)</span></span>&#123;<span class="comment">//处理第一种情况</span></span><br><span class="line">    <span class="type">int</span> res=<span class="number">0</span>;</span><br><span class="line">    ta=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i].v,w=g[u][i].w;</span><br><span class="line">        <span class="keyword">if</span>(!removed[v])&#123;</span><br><span class="line">            tail=<span class="number">0</span>;</span><br><span class="line">            <span class="built_in">get_dis</span>(v,u,w);</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=tail;j++)&#123;<span class="comment">//统计</span></span><br><span class="line">                <span class="keyword">if</span>(dis[j]&lt;=k)res+=mark.<span class="built_in">query</span>(k-dis[j]);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=tail;j++)&#123;<span class="comment">//加入</span></span><br><span class="line">                <span class="keyword">if</span>(dis[j]&lt;=k)&#123;</span><br><span class="line">                    mark.<span class="built_in">add</span>(dis[j],<span class="number">1</span>);</span><br><span class="line">                    res++;</span><br><span class="line">                    vis[++ta]=dis[j];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=ta;i++)&#123;<span class="comment">//复原树状数组方便下次使用</span></span><br><span class="line">        <span class="keyword">if</span>(vis[i]&lt;=k)&#123;</span><br><span class="line">            mark.<span class="built_in">add</span>(vis[i],<span class="number">-1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="1-3-合"><a href="#1-3-合" class="headerlink" title="1.3. 合"></a>1.3. 合</h3><p>只需要无脑地把每种情况加起来就可以了（</p>
<h3 id="1-4-“细节”"><a href="#1-4-“细节”" class="headerlink" title="1.4. “细节”"></a>1.4. “细节”</h3><p>在递归时一定要用子树的重心作为根节点，这样才能保证时间复杂度最优。</p>
<p>至于证明，<del>关于此，我确信已发现了一种美妙的证法 ，可惜这里空白的地方太小，写不下。</del> <del>其实是我太菜了不会证TwT</del> 可以感性理解一下，根节点取重心可以使问题分割地尽可能均匀，分治就跑得飞快了。</p>
<h2 id="2-时间复杂度"><a href="#2-时间复杂度" class="headerlink" title="2. 时间复杂度"></a>2. 时间复杂度</h2><p>据说是 $O(n\log^2 n)$ 的，然而我不会证啊qaq</p>
<p>如果递归时根节点不取重心，时间复杂度会退化为 $O(n^2\log n)$，还不如暴力。</p>
<h2 id="3-Code"><a href="#3-Code" class="headerlink" title="3. Code"></a>3. Code</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 40000</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXW 1000</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INF 0x3fffffff</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">BIT</span>&#123;<span class="comment">//一般通过树状数组</span></span><br><span class="line">    <span class="type">int</span> tree[MAXN*MAXW+<span class="number">5</span>];</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">query</span><span class="params">(<span class="type">int</span> x)</span></span>&#123;</span><br><span class="line">        <span class="type">int</span> res=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span>(x)&#123;</span><br><span class="line">            res+=tree[x];</span><br><span class="line">            x-=x&amp;-x;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">add</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> k)</span></span>&#123;</span><br><span class="line">        <span class="keyword">while</span>(x&lt;=MAXN*MAXW+<span class="number">2</span>)&#123;</span><br><span class="line">            tree[x]+=k;</span><br><span class="line">            x+=x&amp;-x;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="type">int</span> n,k;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">edge</span>&#123;</span><br><span class="line">    <span class="type">int</span> v,w;</span><br><span class="line">    <span class="built_in">edge</span>()&#123;v=w=<span class="number">0</span>;&#125;</span><br><span class="line">    <span class="built_in">edge</span>(<span class="type">int</span> _v,<span class="type">int</span> _w)&#123;v=_v;w=_w;&#125;</span><br><span class="line">&#125;;</span><br><span class="line">vector&lt;edge&gt; g[MAXN+<span class="number">5</span>];</span><br><span class="line"><span class="type">bool</span> removed[MAXN+<span class="number">5</span>];<span class="comment">//由于每个点在递归之后就没用了，所以要打个标记把它移除掉</span></span><br><span class="line"><span class="type">int</span> zx,zx_maxx=INF,siz[MAXN+<span class="number">5</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">get_zx</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> fa,<span class="type">int</span> tot)</span></span>&#123;<span class="comment">//求重心</span></span><br><span class="line">    siz[u]=<span class="number">1</span>;</span><br><span class="line">    <span class="type">int</span> maxx=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i].v;</span><br><span class="line">        <span class="keyword">if</span>(!removed[v]&amp;&amp;v!=fa)&#123;</span><br><span class="line">            <span class="built_in">get_zx</span>(v,u,tot);</span><br><span class="line">            siz[u]+=siz[v];</span><br><span class="line">            maxx=<span class="built_in">max</span>(maxx,siz[v]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    maxx=<span class="built_in">max</span>(maxx,tot-siz[u]);</span><br><span class="line">    <span class="keyword">if</span>(zx_maxx&gt;maxx)&#123;</span><br><span class="line">        zx=u;</span><br><span class="line">        zx_maxx=maxx;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> dis[MAXN+<span class="number">5</span>],tail;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">get_dis</span><span class="params">(<span class="type">int</span> u,<span class="type">int</span> fa,<span class="type">int</span> now)</span></span>&#123;<span class="comment">//求节点到每一个其他点的距离</span></span><br><span class="line">    dis[++tail]=now;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i].v,w=g[u][i].w;</span><br><span class="line">        <span class="keyword">if</span>(!removed[v]&amp;&amp;v!=fa)&#123;</span><br><span class="line">            <span class="built_in">get_dis</span>(v,u,now+w);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">BIT mark;</span><br><span class="line"><span class="type">int</span> vis[MAXN+<span class="number">5</span>],ta;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">calc</span><span class="params">(<span class="type">int</span> u)</span></span>&#123;<span class="comment">//求当前子树经过根节点的合法路径数</span></span><br><span class="line">    <span class="type">int</span> res=<span class="number">0</span>;</span><br><span class="line">    ta=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i].v,w=g[u][i].w;</span><br><span class="line">        <span class="keyword">if</span>(!removed[v])&#123;</span><br><span class="line">            tail=<span class="number">0</span>;</span><br><span class="line">            <span class="built_in">get_dis</span>(v,u,w);</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=tail;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(dis[j]&lt;=k)res+=mark.<span class="built_in">query</span>(k-dis[j]);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=tail;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(dis[j]&lt;=k)&#123;</span><br><span class="line">                    mark.<span class="built_in">add</span>(dis[j],<span class="number">1</span>);</span><br><span class="line">                    res++;</span><br><span class="line">                    vis[++ta]=dis[j];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=ta;i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(vis[i]&lt;=k)&#123;</span><br><span class="line">            mark.<span class="built_in">add</span>(vis[i],<span class="number">-1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">work</span><span class="params">(<span class="type">int</span> u)</span></span>&#123;<span class="comment">//分治</span></span><br><span class="line">    removed[u]=<span class="literal">true</span>;</span><br><span class="line">    <span class="type">int</span> res=<span class="built_in">calc</span>(u);</span><br><span class="line">    <span class="comment">//cout&lt;&lt;u&lt;&lt;&quot; &quot;&lt;&lt;res&lt;&lt;endl;</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;g[u].<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">        <span class="type">int</span> v=g[u][i].v,w=g[u][i].w;</span><br><span class="line">        <span class="keyword">if</span>(!removed[v])&#123;</span><br><span class="line">            zx_maxx=INF;</span><br><span class="line">            <span class="built_in">get_zx</span>(v,<span class="number">0</span>,siz[v]);</span><br><span class="line">            res+=<span class="built_in">work</span>(zx);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>);</span><br><span class="line">    cin&gt;&gt;n;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n<span class="number">-1</span>;i++)&#123;</span><br><span class="line">        <span class="type">int</span> u,v,w;</span><br><span class="line">        cin&gt;&gt;u&gt;&gt;v&gt;&gt;w;</span><br><span class="line">        g[u].<span class="built_in">push_back</span>(<span class="built_in">edge</span>(v,w));</span><br><span class="line">        g[v].<span class="built_in">push_back</span>(<span class="built_in">edge</span>(u,w));</span><br><span class="line">    &#125;</span><br><span class="line">    cin&gt;&gt;k;</span><br><span class="line">    zx_maxx=INF;</span><br><span class="line">    <span class="built_in">get_zx</span>(<span class="number">1</span>,<span class="number">0</span>,n);</span><br><span class="line">    cout&lt;&lt;<span class="built_in">work</span>(zx)&lt;&lt;endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> <del><em>4. 点一个赞！</em></del></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/08/11/%E7%82%B9%E5%88%86%E6%B2%BB%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-id="clk9o8ln8000h8wi50j3n503o" data-title="点分治 学习笔记" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/01/15/%E8%BE%B9%E5%B8%A6%E6%9D%83%E5%B9%B6%E6%9F%A5%E9%9B%86%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20&amp;%20%E6%B4%9B%E8%B0%B7P1196%20%E3%80%8CNOI2002%E3%80%8D%20%E9%93%B6%E6%B2%B3%E8%8B%B1%E9%9B%84%E4%BC%A0%E8%AF%B4%20%E9%A2%98%E8%A7%A3/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          边带权并查集 学习笔记 &amp;amp; 洛谷P1196 「NOI2002」 银河英雄传说 题解
        
      </div>
    </a>
  
  
    <a href="/2021/07/30/UVA1619%20%E6%84%9F%E8%A7%89%E4%B8%8D%E9%94%99%20Feel%20Good%EF%BC%88%E8%89%AF%E5%A5%BD%E7%9A%84%E6%84%9F%E8%A7%89%EF%BC%89%20%E9%A2%98%E8%A7%A3/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">UVA1619 感觉不错 Feel Good（良好的感觉） 题解</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>